package xyz.xtt.exchange.utils;

import java.io.Serializable;

import lombok.Data;
import xyz.xtt.exchange.constants.ErrorMessage;

@Data
public class ApiResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final int SUCCESS = 0;

	private int code;

	private String msg;

	private T data;

	public ApiResult() {

	}

	public ApiResult(T data) {
		this.code = SUCCESS;
		this.msg = "OK";
		this.data = data;
	}

	public ApiResult(Integer status, String msg, T data) {
		this.code = status;
		this.msg = msg;
		this.data = data;
	}

	public static ApiResult<Void> ok() {
		return new ApiResult<>(null);
	}

	public static <T> ApiResult<T> ok(T data) {
		return new ApiResult<>(data);
	}

	public static ApiResult<String> error(ErrorMessage errorCodeEnum) {
		return new ApiResult<>(errorCodeEnum.getCode(), errorCodeEnum.getMessage(), "");
	}

	public static ApiResult<String> build(Integer status, String msg) {
		return new ApiResult<>(status, msg, null);
	}

	public static ApiResult<String> failure(Integer status, String msg) {
		return new ApiResult<>(status, msg, null);
	}

	public static <T> ApiResult<T> build(Integer status, String msg, T data) {
		return new ApiResult<>(status, msg, data);
	}

}
